<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/5/30
 * Time: 下午1:51
 */

namespace App\Http\Controllers\Api\Shop;



use App\Http\Controllers\Controller;

use App\Libs\Pay\WechatPay;
use App\Modules\Order\Services\OrderService;
use App\Modules\Shop\Services\GiftSendService;
use App\Modules\User\Services\UserService;
use Illuminate\Http\Request;


class WechatPayController extends Controller
{
    /**
     * 微信支付回调
     *
     * @param Request $request
     */
   public function wechat_callback(Request $request) {
       $app = OrderService::getWechatPayInstance();
       $response = $app->handlePaidNotify(function($message, $fail){
           // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
           $order =OrderService::findByTradeNo($message['out_trade_no']);

           if (!$order || $order->status == OrderService::ORDER_STATUS_PAID) { // 如果订单不存在 或者 订单已经支付过了
               return true; // 告诉微信，我已经处理完了，订单没找到，别再通知我了
           }

           ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况，确认是已经支付 /////////////

           if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态，不代表支付状态
               // 用户是否支付成功
               if (array_get($message, 'result_code') === 'SUCCESS') {
                   $order->pay_end_at = date('Y-m-d H:i:s',time()); // 更新支付时间为当前时间
                   $order->status = OrderService::ORDER_STATUS_PAID;
                   $order->transaction_id = $message['transaction_id'];
                   $order->save();
                   #
                   $order_products = $order->order_products;
                   foreach ($order_products as $key=>$order_product) {
                       $order_product->status = OrderService::ORDER_STATUS_PAID;
                       $order_product->save();
                       if($order_product->is_vip_product ==1) { //处理购买VIP套餐
                           if($order_product->is_gift==1){
                               $gift_send = GiftSendService::getByOrderProductId($order_product->id);
                               $gift_send->status = 1;
                               $gift_send->save();
                           }else {
                               $vip_month = $order_product->product->vip_month;
                               $user_info = UserService::getById($order->uid);
                               $user_info->vip =1;
                               $user_info->vip_start_at = date('Y-m-d H:i:s');
                               $user_info->vip_end_at = date('Y-m-d H:i:s',strtotime('+'.$vip_month.' month'));
                               $user_info->save();
                           }
                       }
                   }

                   // 用户支付失败
               } elseif (array_get($message, 'result_code') === 'FAIL') {
                   $order->status = OrderService::ORDER_STATUS_FAILED;
               }
           } else {
               return $fail('通信失败，请稍后再通知我');
           }

           $order->save(); // 保存订单

           return true; // 返回处理完成
       });

       $response->send(); // return $response;

   }
}